package com.hr.user.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.hr.user.dao.Impl.UserDaoImpl;
import com.hr.user.domain.User;
import com.hr.user.service.Impl.UserServiceImpl;
import com.hr.utils.CommonUtils;

public class UserAddServlet extends HttpServlet {
	private UserServiceImpl userServiceImpl = new UserServiceImpl();
	private UserServlet userServlet = new UserServlet();
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		/**
		 * 1、使用上传三步，把表单数据封装到Book对象中
		 */
		//创建工厂
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//获得解析器
		ServletFileUpload sfu = new ServletFileUpload(factory);
		
		//使用解析器解析request对象，得到List<FileItem>
		try {
			List<FileItem> fileItemList = sfu.parseRequest(request);
			//封装fileItemList中的相关的数据到Book对象中
			/**
			 * 先将普通表单字段封装到Map中
			 * 再把Map中的数据封装到Book对象中
			 */
			//封装普通数据到book对象中之后在进行文件路径的保存和文件的自身的上传
			Map<String , String> map = new HashMap<String,String>();
			for (FileItem fileItem : fileItemList) {
				if(fileItem.isFormField()){
					map.put(fileItem.getFieldName(), fileItem.getString("utf-8"));
				}
			}
			
			User user = CommonUtils.toBean(map, User.class);
			user.setUser_id(CommonUtils.uuid());
			
			//保证上传文件的前缀和用户的ID是一致的，并且保证唯一性
			String savePath_photo = this.getServletContext().getRealPath("/uploadPhoto");
			String photoname = user.getUser_id() + "_" + fileItemList.get(6).getName();
			String savePath_file = this.getServletContext().getRealPath("/uploadFile");
			String filename = user.getUser_id() + "_" + fileItemList.get(7).getName();
			user.setFilename(fileItemList.get(7).getName());
			
			File destFile_photo = new File(savePath_photo,photoname);
			File destFile_file = new File(savePath_file,filename);
			
			try {
				fileItemList.get(6).write(destFile_photo);
				fileItemList.get(7).write(destFile_file);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			user.setPhoto("uploadPhoto/"+photoname);
			user.setUserfile("uploadFile/"+filename);
			
			userServiceImpl.addUser(user);
			request.getRequestDispatcher("UserServlet?method=queryAllEmp").forward(request, response);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
